﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatewayInspect.Common;
using System.Data.OracleClient;

namespace GatewayInspect.Modules
{
    public partial class SKModule : BaseModule
    {
        RFIDReader reader = null;

        public SKModule()
        {
            InitializeComponent();
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            foreach (ToolStripItem item in this.tsMain.Items)
            {
                if (item != tsBtnClose)
                {
                    item.Visible = false;
                }
            }
        }

        protected override void OnInit()
        {
            base.OnInit();
            BeginRead();//连接读卡设备开始读卡
        }

        protected override void OnModuleDestroyed()
        {
            base.OnModuleDestroyed();
            if (reader != null)
            {
                reader.AfterReadCardInfo -= new RFIDReader.AfterReadCardInfoHandler(reader_AfterReadCardInfo);
                reader.CheckHandler();//检查是否还有其他对象读卡
            }
        }

        private void BeginRead()
        {
            if (reader == null)
            {
                reader = RFIDReader.GetInstance();
                reader.AfterReadCardInfo += new RFIDReader.AfterReadCardInfoHandler(reader_AfterReadCardInfo);
            }
            reader.BeginRead();
        }

        private void reader_AfterReadCardInfo(string strTid, string strEPC)
        {
            if (!string.IsNullOrEmpty(strTid) && strTid != tbxCardNo.Text)
            {
                tbxCardNo.Text = strTid;
            }
        }

        private void btnBeginRead_Click(object sender, EventArgs e)
        {
            BeginRead();
        }

        private void btnEndRead_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                reader.EndRead();
            }
            MessageBox.Show("已停止读卡！");
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string strCardNo = this.tbxCardNo.Text.Trim();
            if (string.IsNullOrEmpty(strCardNo))
            {
                MessageBox.Show("卡号不能为空！");
                return;
            }
            DataTable dtRFID = DataAccess.ExecuteDataTable("select CHEHAO from RFID_READ where CARDSTATE = '0' and CARDID = '" + strCardNo + "'");
            if (dtRFID == null || dtRFID.Rows.Count <= 0)
            {
                MessageBox.Show("该卡未与任何车辆关联！");
                return;
            }
            DataAccess.ExecuteNonQuery("update RFID_READ set CARDSTATE='1' where CARDSTATE = '0' and CARDID = '" + strCardNo + "'");//收卡
            //更新出港时间
            string strTruckNo = dtRFID.Rows[0]["CHEHAO"].ToString();
            DataAccess.ExecuteNonQuery("update T_TRUCK_GATEINSPECT set OUTTIME=SYSDATE WHERE TRUCKNO='" + strTruckNo + "' and PASSED='1' and OUTTIME is null");
            MessageBox.Show("收卡成功！");
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(tbxCardNo.Text))
            {
                string strSql = "select CARDID,CHEHAO,SENDCARDTIME,SENDCARDMAN,HD_WEIGHT from RFID_READ where CARDSTATE = '0' and CARDID = '" + tbxCardNo.Text.Trim() + "'";
                cdgvFK.DataSource = DataAccess.ExecuteDataTable(strSql);
            }
            else
            {
                MessageBox.Show("卡号不能为空！");
            }
        }
    }
}
